package com.DeliveryProgram.www.dao;

import com.DeliveryProgram.www.po.Account;
import com.DeliveryProgram.www.po.Business;
import com.DeliveryProgram.www.po.User;
import com.DeliveryProgram.www.util.JDBCutil;

import java.sql.*;

/**操作数据库中账户数据*/
public class Acot {

    private Account account;



/**验证账号密码是否正确*/
    public boolean compare()  {

        Connection conn=null;
        PreparedStatement prep=null;
        ResultSet rs=null;
        try {
            conn=JDBCutil.getConnection();
            String sql="select name,password from account where name=? and password=?";
            prep= conn.prepareCall(sql);
            prep.setString(1,account.getName());
            prep.setString(2,account.getPassword());
            rs= prep.executeQuery();
            return rs.next();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(prep!=null){
                try {
                    prep.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


        }


        return false;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    /**从数据库获取用户类型*/
    public void setUser(){
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        try {
            conn=JDBCutil.getConnection();
            String sql="select * from account where name ='"+account.getName()+"'";
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            rs.next();
            account.setUser(rs.getInt("user"));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(rs,stmt,conn);
        }
    }

    /**获取用户类型*/
    public int getUser(){
        return account.getUser();
    }

    /**向数据库中注册用户*/
    public boolean enroll(){
        Connection conn=null;
        ResultSet rs=null;
        Statement stmt=null;

        try {
            conn=JDBCutil.getConnection();
            String sql="INSERT INTO account(NAME) VALUES ('"+account.getName()+"')";
            stmt=conn.createStatement();
            stmt.execute(sql);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(rs,stmt,conn);
        }


        return false;
    }

    /**向数据库中存入用户密码和用户类型*/
    public boolean messageOut(String password,int user){

        /**保存用户类型，为后续操作做准备*/
        account.setUser(user);

        Connection conn=null;
        Statement stmt=null;


        try {
            conn=JDBCutil.getConnection();
            String sql="UPDATE account SET PASSWORD='"+password+"',user="+user+" WHERE name='"+account.getName()+"'";
            stmt=conn.createStatement();
            int comp=stmt.executeUpdate(sql);
            loading();
            if(comp==0){
                return false;
            }else{
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCutil.close(stmt,conn);
        }
        return false;
    }

    /**在用户表或商户表注册*/
    public void loading(){
        Connection conn=null;
        Statement stmt=null;

        try {
            conn=JDBCutil.getConnection();
            String sql1="INSERT INTO business(NAME) VALUES('"+account.getName()+"')";
            String sql2="INSERT INTO user(NAME,money) VALUES('"+account.getName()+"',0)";
            stmt=conn.createStatement();
            if(account.getUser()==1){
                stmt.execute(sql2);
            }else{
                stmt.execute(sql1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCutil.close(stmt,conn);
        }

    }

    /**获取内存中用户姓名*/
    public String getUserNAme() {
        return account.getName();
    }

    /**获取商户对象*/
    public Business getBusiness(){
        Business bus=new Business(getUserNAme());
        BusLoad.Read(bus);

        /**对初次登录的商户设定商户基本信息*/
        if(bus.getShopname()==null){
            /**接受店名输入的结果*/
            boolean bool =false;
            while(!bool){
                bool=BusLoad.Create(bus);
            }
            return bus;

        }

        /**读取商户对象数据库中的菜单*/
        BusLoad.loadMenu(bus);

        /**获取该商家设置的折扣*/
        BusLoad.loadCoupons(bus);

        /**获取商家的订单记录*/
        BusLoad.loadOrder(bus);

        /**获取商家累计收益*/
        BusLoad.loadPrice(bus);

        /**获取商家的评论*/
        BusLoad.loadBuComment(bus);

        /**获取用户对商家的评论*/
        BusLoad.loadUsComment(bus);

        return bus;
    };

    /**获取用户对象*/
    public User getCustom(){

        User user=new User();
        user.setName(getUserNAme());

        /**获取基础信息*/
        UserLoad.getUser(user);

        /**获取用户拥有的折扣*/
        UserLoad.getCoupons(user);
        /**获取用户的地址*/
        UserLoad.getAdress(user);
        /**获取用户的订单*/
        UserLoad.getOrders(user);
        /**获取用户的评论*/
        UserLoad.getComment(user);

        return user;
    }

}
